package com.yonyou.pmclouds.discuss.impl;

import com.yonyou.pmclouds.alter.entity.AlterVO;
import com.yonyou.pmclouds.alter.rmiitf.AlterQuery;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.discuss.entity.DiscussionVO;
import com.yonyou.pmclouds.discuss.itf.AbstractSpecifiedTypeDiscussProcessor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AlterDiscussProcessor extends AbstractSpecifiedTypeDiscussProcessor {
    private static final String ALTER_MESSAGE_CREATOR = "了您发起的【变更申请】";
    private static final String ALTER_MESSAGE_COMMENT = "了您评论的【变更申请】";
    private static final String ALTER_MESSAGE_REPLY = "了您回复的【变更申请】";
    private static final String ALTER_MESSAGE_AT = "在【变更申请】@了您";

    @Autowired
    private AlterQuery query;

    @Override
    protected Map<String, String> getUserTitle(DiscussionVO discussion) throws BusinessException {
        AlterVO alterVO = query.getById(discussion.getRelateKey());
        Map<String, String> result = new HashMap<>();
        // get @ users
        String[] atUsers = extractMentionedUser(discussion.getContent());
        String atMessage = discussion.getCreatorName() + ALTER_MESSAGE_AT;
        for (String atUser : atUsers) {
            result.put(atUser, atMessage);
        }
        int type = getDiscussType(discussion);
        String msgHead = discussion.getCreatorName() + getDiscussTypeMessage(type);
        if (type == REPLY_TYPE) {
            // 回复，需要被回复人和评论人消息
            if (!StringUtils.isEmpty(discussion.getRepliedUser())) {
                result.put(discussion.getRepliedUser(),
                        msgHead + ALTER_MESSAGE_REPLY);
            }
            DiscussionVO repliedDiscuss = getRepliedDiscuss(discussion);
            if (repliedDiscuss != null) {
                result.put(repliedDiscuss.getCreator(),
                        msgHead + ALTER_MESSAGE_COMMENT);
            }
        }
        result.put(alterVO.getCreator(), msgHead + ALTER_MESSAGE_CREATOR);
        return result;
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.ALTER_BILLTYPE;
    }
}
